from openai import AsyncOpenAI

from src.utils import get_config
from typings import ChatCompletionMessageParam, ChatCompletionToolParam, NotGiven


llm_configs = get_config().llm_configs
llm_clients = {
    config.name: AsyncOpenAI(api_key=config.key, base_url=config.url) for config in llm_configs
}


async def create_client(
    message: list[ChatCompletionMessageParam],
    functools: list[ChatCompletionToolParam] | NotGiven | None = None,
): 
    for config in llm_configs:
        if functools is None:
            functools = NotGiven()
        if client := llm_clients.get(config.name):
            response = await client.chat.completions.create(
                model=config.model,
                messages=message,
                functions=functools,
                max_tokens=2048,
                temperature=0.1,
            )
            return response
